package com.rfid.emergencywarehouse.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.Snackbar;
import com.rfid.base.util.AnimatorHelper;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.GetAssetAdapter;
import com.rfid.emergencywarehouse.adapter.ScanBarcodeAdapter;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.presenter.GetAssetPresenter;
import com.rfid.emergencywarehouse.view.IGetAssetView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public abstract class GetAssetFragment<T extends GetAssetPresenter> extends BaseScanListFragment<GetAssetAdapter, BarcodeInfo, T> implements IGetAssetView {

    @BindView(R.id.barcode)
    EditText mBarcodeEdit;
    @BindView(R.id.root)
    CoordinatorLayout mRoot;

    protected String mOrderId;
    @Override
    protected int getScanRfidCheckBoxId() {
        return R.id.scan;
    }

    @Override
    protected String getScanTip(int size) {
        return String.format("已扫描数据%s条，已选中%s条",size,mSelects.size());
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("无单领取");
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
    }

    @Override
    protected EditText getBarcodeEdit() {
        return mBarcodeEdit;
    }

    @Override
    protected GetAssetAdapter getAdapter() {
        return new GetAssetAdapter(mData);
    }


    @Override
    protected boolean needSelect(BarcodeInfo info) {
        return !info.isOutSide()&&info.isEnable();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_get_asset;
    }

    @Override
    public void onDoGetAssetResult(boolean succes, String msg) {
        Context context = getContext();
        if (context == null) {
            return;
        }
        if (succes) {
            showSuccess("领取成功");
            clearSelectData();
            mBarcodeEdit.setText("");
        } else {
            showTip(msg);
//            Snackbar.make(mRoot,msg,Snackbar.LENGTH_SHORT).show();
        }
    }

    @OnClick(R.id.right_btn)
    public void right_button(View v) {
//        String barcode = mBarcodeEdit.getText().toString();
//        if (TextUtils.isEmpty(barcode)) {
//            AnimatorHelper.shake(getContext(),mBarcodeEdit);
//            return;
//        }
        List<String> barcodes = getBarcodes();
        if (barcodes == null) {
            return;
        }
        mPresenter.doGetAsset(mOrderId,barcodes);
    }

    @Override
    protected boolean needAdd(BarcodeInfo info) {
        return !info.isNew();
    }
}
